package net.ge.mosq.web.servlet;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ge.mosq.jdo.entity.AccountItem;
import net.ge.mosq.jdo.entity.AccountType;
import net.ge.mosq.jdo.entity.TransactionInfo;
import net.ge.mosq.jdo.entity.UserInfo;
import net.ge.mosq.service.ServiceFactory;
import net.ge.mosq.service.impl.TransactionService;
import net.ge.mosq.web.others.ActionMessages;
import net.ge.mosq.web.servlet.form.TransactionForm;

import org.apache.commons.lang.StringUtils;

/**
 * @author noodles.wumu@gmail.com
 * @since 2012/07/18 13:28:50
 * @version 1.0
 */
public class TransServlet extends BaseServlet {

	private static final long serialVersionUID = 3839254939784438056L;

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// response.setContentType("text/plain");
		// response.setContentType("application/json");

		TransactionForm form = (TransactionForm) generateForm(TransactionForm.class, req);
		

		if (StringUtils.isNotBlank(form.getActionFlg())) {
			// if (actionFlg.equals("0")) {
			AccountItem accountItem = ServiceFactory.getAccountService().getAnAccountByKey(Long.parseLong(form.getCurrentAccId()));

			Map<String, AccountType> accountTypes = (Map<String, AccountType>) getServletContext().getAttribute("AccountTypes");

			form.setLabel1(accountTypes.get(accountItem.getType()).getLabel1());
			form.setLabel2(accountTypes.get(accountItem.getType()).getLabel2());
			form.setTransIds(ServiceFactory.getAccountService().getAccountNameWithAllParent(((UserInfo) req.getSession().getAttribute("userInfo")).getEmail()));
			// }
			if (form.getActionFlg().equals("1")) {
				ActionMessages ams = form.validate(req);
				if (ams.isEmpty()) {
					SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
					TransactionInfo transInfo = new TransactionInfo();
					try {
						transInfo.setTransDate(df.parse(form.getTransDate()));
					} catch (ParseException e) {
					}
					if (StringUtils.isNotEmpty(form.getNum())) {
						transInfo.setNum(Integer.parseInt(form.getNum()));
					}
					transInfo.setDescription(form.getDescription());

					BigDecimal plus = form.getPlus() == null ? BigDecimal.ZERO : new BigDecimal(form.getPlus());
					BigDecimal minus = form.getMinus() == null ? BigDecimal.ZERO : new BigDecimal(form.getMinus());

					if (plus.compareTo(minus) > 0) {
						transInfo.setPlusAccId(Long.parseLong(form.getCurrentAccId()));
						transInfo.setMinusAccId(Long.parseLong(form.getTransAccId()));
					} else {
						transInfo.setPlusAccId(Long.parseLong(form.getTransAccId()));
						transInfo.setMinusAccId(Long.parseLong(form.getCurrentAccId()));
					}
					transInfo.setTransAmount(plus.subtract(minus).abs());
					new TransactionService().create(transInfo);
				}
			}
			List<TransactionInfo> transList = new TransactionService().getTransactionInfoList(Long.parseLong(form.getCurrentAccId()));
			req.setAttribute("transList", transList);
		}

		req.getRequestDispatcher("/trans.jsp?t="+new Date().getTime()).forward(req, resp);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}

}
